<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.sampling.Sampling &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.sampling.Sampling</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">namedtuple</span>
<span class="kn">import</span> <span class="nn">itertools</span>

<span class="kn">import</span> <span class="nn">networkx</span> <span class="k">as</span> <span class="nn">nx</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">pgmpy.factors</span> <span class="k">import</span> <span class="n">factor_product</span>
<span class="kn">from</span> <span class="nn">pgmpy.inference</span> <span class="k">import</span> <span class="n">Inference</span>
<span class="kn">from</span> <span class="nn">pgmpy.models</span> <span class="k">import</span> <span class="n">BayesianModel</span><span class="p">,</span> <span class="n">MarkovChain</span><span class="p">,</span> <span class="n">MarkovModel</span>
<span class="kn">from</span> <span class="nn">pgmpy.utils.mathext</span> <span class="k">import</span> <span class="n">sample_discrete</span>
<span class="kn">from</span> <span class="nn">pgmpy.extern.six.moves</span> <span class="k">import</span> <span class="nb">map</span><span class="p">,</span> <span class="nb">range</span>
<span class="kn">from</span> <span class="nn">pgmpy.sampling</span> <span class="k">import</span> <span class="n">_return_samples</span>


<span class="n">State</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;State&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;var&#39;</span><span class="p">,</span> <span class="s1">&#39;state&#39;</span><span class="p">])</span>


<div class="viewcode-block" id="BayesianModelSampling"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.Sampling.BayesianModelSampling">[docs]</a><span class="k">class</span> <span class="nc">BayesianModelSampling</span><span class="p">(</span><span class="n">Inference</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Class for sampling methods specific to Bayesian Models</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    model: instance of BayesianModel</span>
<span class="sd">        model on which inference queries will be computed</span>


<span class="sd">    Public Methods</span>
<span class="sd">    --------------</span>
<span class="sd">    forward_sample(size)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">BayesianModel</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Model expected type: BayesianModel, got type: &quot;</span><span class="p">,</span> <span class="nb">type</span><span class="p">(</span><span class="n">model</span><span class="p">))</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">topological_order</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">nx</span><span class="o">.</span><span class="n">topological_sort</span><span class="p">(</span><span class="n">model</span><span class="p">))</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">BayesianModelSampling</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>

<div class="viewcode-block" id="BayesianModelSampling.forward_sample"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.Sampling.BayesianModelSampling.forward_sample">[docs]</a>    <span class="k">def</span> <span class="nf">forward_sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">return_type</span><span class="o">=</span><span class="s1">&#39;dataframe&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates sample(s) from joint distribution of the bayesian network.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        size: int</span>
<span class="sd">            size of sample to be generated</span>

<span class="sd">        return_type: string (dataframe | recarray)</span>
<span class="sd">            Return type for samples, either of &#39;dataframe&#39; or &#39;recarray&#39;.</span>
<span class="sd">            Defaults to &#39;dataframe&#39;</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sampled: A pandas.DataFrame or a numpy.recarray object depending upon return_type argument</span>
<span class="sd">            the generated samples</span>


<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models.BayesianModel import BayesianModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import TabularCPD</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.sampling import BayesianModelSampling</span>
<span class="sd">        &gt;&gt;&gt; student = BayesianModel([(&#39;diff&#39;, &#39;grade&#39;), (&#39;intel&#39;, &#39;grade&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; cpd_d = TabularCPD(&#39;diff&#39;, 2, [[0.6], [0.4]])</span>
<span class="sd">        &gt;&gt;&gt; cpd_i = TabularCPD(&#39;intel&#39;, 2, [[0.7], [0.3]])</span>
<span class="sd">        &gt;&gt;&gt; cpd_g = TabularCPD(&#39;grade&#39;, 3, [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25,</span>
<span class="sd">        ...                0.08, 0.3], [0.3, 0.7, 0.02, 0.2]],</span>
<span class="sd">        ...                [&#39;intel&#39;, &#39;diff&#39;], [2, 2])</span>
<span class="sd">        &gt;&gt;&gt; student.add_cpds(cpd_d, cpd_i, cpd_g)</span>
<span class="sd">        &gt;&gt;&gt; inference = BayesianModelSampling(student)</span>
<span class="sd">        &gt;&gt;&gt; inference.forward_sample(size=2, return_type=&#39;recarray&#39;)</span>
<span class="sd">        rec.array([(0, 0, 1), (1, 0, 2)], dtype=</span>
<span class="sd">                  [(&#39;diff&#39;, &#39;&lt;i8&#39;), (&#39;intel&#39;, &#39;&lt;i8&#39;), (&#39;grade&#39;, &#39;&lt;i8&#39;)])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">types</span> <span class="o">=</span> <span class="p">[(</span><span class="n">var_name</span><span class="p">,</span> <span class="s1">&#39;int&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">var_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">topological_order</span><span class="p">]</span>
        <span class="n">sampled</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">size</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">types</span><span class="p">)</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">recarray</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">topological_order</span><span class="p">:</span>
            <span class="n">cpd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">get_cpds</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
            <span class="n">states</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">node</span><span class="p">])</span>
            <span class="n">evidence</span> <span class="o">=</span> <span class="n">cpd</span><span class="o">.</span><span class="n">variables</span><span class="p">[:</span><span class="mi">0</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">evidence</span><span class="p">:</span>
                <span class="n">cached_values</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pre_compute_reduce</span><span class="p">(</span><span class="n">variable</span><span class="o">=</span><span class="n">node</span><span class="p">)</span>
                <span class="n">evidence</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">([</span><span class="n">sampled</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">evidence</span><span class="p">])</span>
                <span class="n">weights</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">cached_values</span><span class="p">[</span><span class="nb">tuple</span><span class="p">(</span><span class="n">t</span><span class="p">)],</span> <span class="n">evidence</span><span class="o">.</span><span class="n">T</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">weights</span> <span class="o">=</span> <span class="n">cpd</span><span class="o">.</span><span class="n">values</span>
            <span class="n">sampled</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">sample_discrete</span><span class="p">(</span><span class="n">states</span><span class="p">,</span> <span class="n">weights</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">_return_samples</span><span class="p">(</span><span class="n">return_type</span><span class="p">,</span> <span class="n">sampled</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">pre_compute_reduce</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">variable</span><span class="p">):</span>
        <span class="n">variable_cpd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">get_cpds</span><span class="p">(</span><span class="n">variable</span><span class="p">)</span>
        <span class="n">variable_evid</span> <span class="o">=</span> <span class="n">variable_cpd</span><span class="o">.</span><span class="n">variables</span><span class="p">[:</span><span class="mi">0</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">cached_values</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">for</span> <span class="n">state_combination</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">var</span><span class="p">])</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">variable_evid</span><span class="p">]):</span>
            <span class="n">states</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">variable_evid</span><span class="p">,</span> <span class="n">state_combination</span><span class="p">))</span>
            <span class="n">cached_values</span><span class="p">[</span><span class="n">state_combination</span><span class="p">]</span> <span class="o">=</span> <span class="n">variable_cpd</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">states</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span><span class="o">.</span><span class="n">values</span>

        <span class="k">return</span> <span class="n">cached_values</span>

<div class="viewcode-block" id="BayesianModelSampling.rejection_sample"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.Sampling.BayesianModelSampling.rejection_sample">[docs]</a>    <span class="k">def</span> <span class="nf">rejection_sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">evidence</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">return_type</span><span class="o">=</span><span class="s2">&quot;dataframe&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates sample(s) from joint distribution of the bayesian network,</span>
<span class="sd">        given the evidence.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        evidence: list of `pgmpy.factor.State` namedtuples</span>
<span class="sd">            None if no evidence</span>
<span class="sd">        size: int</span>
<span class="sd">            size of sample to be generated</span>
<span class="sd">        return_type: string (dataframe | recarray)</span>
<span class="sd">            Return type for samples, either of &#39;dataframe&#39; or &#39;recarray&#39;.</span>
<span class="sd">            Defaults to &#39;dataframe&#39;</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sampled: A pandas.DataFrame or a numpy.recarray object depending upon return_type argument</span>
<span class="sd">            the generated samples</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models.BayesianModel import BayesianModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import TabularCPD</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import State</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.sampling import BayesianModelSampling</span>
<span class="sd">        &gt;&gt;&gt; student = BayesianModel([(&#39;diff&#39;, &#39;grade&#39;), (&#39;intel&#39;, &#39;grade&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; cpd_d = TabularCPD(&#39;diff&#39;, 2, [[0.6], [0.4]])</span>
<span class="sd">        &gt;&gt;&gt; cpd_i = TabularCPD(&#39;intel&#39;, 2, [[0.7], [0.3]])</span>
<span class="sd">        &gt;&gt;&gt; cpd_g = TabularCPD(&#39;grade&#39;, 3, [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25,</span>
<span class="sd">        ...                0.08, 0.3], [0.3, 0.7, 0.02, 0.2]],</span>
<span class="sd">        ...                [&#39;intel&#39;, &#39;diff&#39;], [2, 2])</span>
<span class="sd">        &gt;&gt;&gt; student.add_cpds(cpd_d, cpd_i, cpd_g)</span>
<span class="sd">        &gt;&gt;&gt; inference = BayesianModelSampling(student)</span>
<span class="sd">        &gt;&gt;&gt; evidence = [State(var=&#39;diff&#39;, state=0)]</span>
<span class="sd">        &gt;&gt;&gt; inference.rejection_sample(evidence=evidence, size=2, return_type=&#39;dataframe&#39;)</span>
<span class="sd">                intel       diff       grade</span>
<span class="sd">        0         0          0          1</span>
<span class="sd">        1         0          0          1</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">evidence</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">forward_sample</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
        <span class="n">types</span> <span class="o">=</span> <span class="p">[(</span><span class="n">var_name</span><span class="p">,</span> <span class="s1">&#39;int&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">var_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">topological_order</span><span class="p">]</span>
        <span class="n">sampled</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">types</span><span class="p">)</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">recarray</span><span class="p">)</span>
        <span class="n">prob</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">while</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">size</span><span class="p">:</span>
            <span class="n">_size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(((</span><span class="n">size</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="o">/</span> <span class="n">prob</span><span class="p">)</span> <span class="o">*</span> <span class="mf">1.5</span><span class="p">)</span>
            <span class="n">_sampled</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">forward_sample</span><span class="p">(</span><span class="n">_size</span><span class="p">,</span> <span class="s1">&#39;recarray&#39;</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">evid</span> <span class="ow">in</span> <span class="n">evidence</span><span class="p">:</span>
                <span class="n">_sampled</span> <span class="o">=</span> <span class="n">_sampled</span><span class="p">[</span><span class="n">_sampled</span><span class="p">[</span><span class="n">evid</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">==</span> <span class="n">evid</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>

            <span class="n">prob</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">_sampled</span><span class="p">)</span> <span class="o">/</span> <span class="n">_size</span><span class="p">,</span> <span class="mf">0.01</span><span class="p">)</span>
            <span class="n">sampled</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">sampled</span><span class="p">,</span> <span class="n">_sampled</span><span class="p">)[:</span><span class="n">size</span><span class="p">]</span>

            <span class="n">i</span> <span class="o">+=</span> <span class="nb">len</span><span class="p">(</span><span class="n">_sampled</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">_return_samples</span><span class="p">(</span><span class="n">return_type</span><span class="p">,</span> <span class="n">sampled</span><span class="p">)</span></div>

<div class="viewcode-block" id="BayesianModelSampling.likelihood_weighted_sample"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.Sampling.BayesianModelSampling.likelihood_weighted_sample">[docs]</a>    <span class="k">def</span> <span class="nf">likelihood_weighted_sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">evidence</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">return_type</span><span class="o">=</span><span class="s2">&quot;dataframe&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generates weighted sample(s) from joint distribution of the bayesian</span>
<span class="sd">        network, that comply with the given evidence.</span>
<span class="sd">        &#39;Probabilistic Graphical Model Principles and Techniques&#39;, Koller and</span>
<span class="sd">        Friedman, Algorithm 12.2 pp 493.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        evidence: list of `pgmpy.factor.State` namedtuples</span>
<span class="sd">            None if no evidence</span>
<span class="sd">        size: int</span>
<span class="sd">            size of sample to be generated</span>
<span class="sd">        return_type: string (dataframe | recarray)</span>
<span class="sd">            Return type for samples, either of &#39;dataframe&#39; or &#39;recarray&#39;.</span>
<span class="sd">            Defaults to &#39;dataframe&#39;</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sampled: A pandas.DataFrame or a numpy.recarray object depending upon return_type argument</span>
<span class="sd">            the generated samples with corresponding weights</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import State</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models.BayesianModel import BayesianModel</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import TabularCPD</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.sampling import BayesianModelSampling</span>
<span class="sd">        &gt;&gt;&gt; student = BayesianModel([(&#39;diff&#39;, &#39;grade&#39;), (&#39;intel&#39;, &#39;grade&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; cpd_d = TabularCPD(&#39;diff&#39;, 2, [[0.6], [0.4]])</span>
<span class="sd">        &gt;&gt;&gt; cpd_i = TabularCPD(&#39;intel&#39;, 2, [[0.7], [0.3]])</span>
<span class="sd">        &gt;&gt;&gt; cpd_g = TabularCPD(&#39;grade&#39;, 3, [[0.3, 0.05, 0.9, 0.5], [0.4, 0.25,</span>
<span class="sd">        ...         0.08, 0.3], [0.3, 0.7, 0.02, 0.2]],</span>
<span class="sd">        ...         [&#39;intel&#39;, &#39;diff&#39;], [2, 2])</span>
<span class="sd">        &gt;&gt;&gt; student.add_cpds(cpd_d, cpd_i, cpd_g)</span>
<span class="sd">        &gt;&gt;&gt; inference = BayesianModelSampling(student)</span>
<span class="sd">        &gt;&gt;&gt; evidence = [State(&#39;diff&#39;, 0)]</span>
<span class="sd">        &gt;&gt;&gt; inference.likelihood_weighted_sample(evidence=evidence, size=2, return_type=&#39;recarray&#39;)</span>
<span class="sd">        rec.array([(0, 0, 1, 0.6), (0, 0, 2, 0.6)], dtype=</span>
<span class="sd">                  [(&#39;diff&#39;, &#39;&lt;i8&#39;), (&#39;intel&#39;, &#39;&lt;i8&#39;), (&#39;grade&#39;, &#39;&lt;i8&#39;), (&#39;_weight&#39;, &#39;&lt;f8&#39;)])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">types</span> <span class="o">=</span> <span class="p">[(</span><span class="n">var_name</span><span class="p">,</span> <span class="s1">&#39;int&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">var_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">topological_order</span><span class="p">]</span>
        <span class="n">types</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="s1">&#39;_weight&#39;</span><span class="p">,</span> <span class="s1">&#39;float&#39;</span><span class="p">))</span>
        <span class="n">sampled</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">size</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">types</span><span class="p">)</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">recarray</span><span class="p">)</span>
        <span class="n">sampled</span><span class="p">[</span><span class="s1">&#39;_weight&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
        <span class="n">evidence_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">var</span><span class="p">:</span> <span class="n">st</span> <span class="k">for</span> <span class="n">var</span><span class="p">,</span> <span class="n">st</span> <span class="ow">in</span> <span class="n">evidence</span><span class="p">}</span>

        <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">topological_order</span><span class="p">:</span>
            <span class="n">cpd</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">get_cpds</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
            <span class="n">states</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinality</span><span class="p">[</span><span class="n">node</span><span class="p">])</span>
            <span class="n">evidence</span> <span class="o">=</span> <span class="n">cpd</span><span class="o">.</span><span class="n">get_evidence</span><span class="p">()</span>

            <span class="k">if</span> <span class="n">evidence</span><span class="p">:</span>
                <span class="n">evidence_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">([</span><span class="n">sampled</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">evidence</span><span class="p">])</span>
                <span class="n">cached_values</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pre_compute_reduce</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
                <span class="n">weights</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">cached_values</span><span class="p">[</span><span class="nb">tuple</span><span class="p">(</span><span class="n">t</span><span class="p">)],</span> <span class="n">evidence_values</span><span class="o">.</span><span class="n">T</span><span class="p">))</span>
                <span class="k">if</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">evidence_dict</span><span class="p">:</span>
                    <span class="n">sampled</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">evidence_dict</span><span class="p">[</span><span class="n">node</span><span class="p">]</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span><span class="p">):</span>
                        <span class="n">sampled</span><span class="p">[</span><span class="s1">&#39;_weight&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">*=</span> <span class="n">weights</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">evidence_dict</span><span class="p">[</span><span class="n">node</span><span class="p">]]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sampled</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">sample_discrete</span><span class="p">(</span><span class="n">states</span><span class="p">,</span> <span class="n">weights</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">node</span> <span class="ow">in</span> <span class="n">evidence_dict</span><span class="p">:</span>
                    <span class="n">sampled</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">evidence_dict</span><span class="p">[</span><span class="n">node</span><span class="p">]</span>
                    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span><span class="p">):</span>
                        <span class="n">sampled</span><span class="p">[</span><span class="s1">&#39;_weight&#39;</span><span class="p">][</span><span class="n">i</span><span class="p">]</span> <span class="o">*=</span> <span class="n">cpd</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="n">evidence_dict</span><span class="p">[</span><span class="n">node</span><span class="p">]]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">sampled</span><span class="p">[</span><span class="n">node</span><span class="p">]</span> <span class="o">=</span> <span class="n">sample_discrete</span><span class="p">(</span><span class="n">states</span><span class="p">,</span> <span class="n">cpd</span><span class="o">.</span><span class="n">values</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">_return_samples</span><span class="p">(</span><span class="n">return_type</span><span class="p">,</span> <span class="n">sampled</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="GibbsSampling"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.Sampling.GibbsSampling">[docs]</a><span class="k">class</span> <span class="nc">GibbsSampling</span><span class="p">(</span><span class="n">MarkovChain</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Class for performing Gibbs sampling.</span>

<span class="sd">    Parameters:</span>
<span class="sd">    -----------</span>
<span class="sd">    model: BayesianModel or MarkovModel</span>
<span class="sd">        Model from which variables are inherited and transition probabilites computed.</span>

<span class="sd">    Public Methods:</span>
<span class="sd">    ---------------</span>
<span class="sd">    set_start_state(state)</span>
<span class="sd">    sample(start_state, size)</span>
<span class="sd">    generate_sample(start_state, size)</span>

<span class="sd">    Examples:</span>
<span class="sd">    ---------</span>
<span class="sd">    Initialization from a BayesianModel object:</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.factors.discrete import TabularCPD</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.models import BayesianModel</span>
<span class="sd">    &gt;&gt;&gt; intel_cpd = TabularCPD(&#39;intel&#39;, 2, [[0.7], [0.3]])</span>
<span class="sd">    &gt;&gt;&gt; sat_cpd = TabularCPD(&#39;sat&#39;, 2, [[0.95, 0.2], [0.05, 0.8]], evidence=[&#39;intel&#39;], evidence_card=[2])</span>
<span class="sd">    &gt;&gt;&gt; student = BayesianModel()</span>
<span class="sd">    &gt;&gt;&gt; student.add_nodes_from([&#39;intel&#39;, &#39;sat&#39;])</span>
<span class="sd">    &gt;&gt;&gt; student.add_edge(&#39;intel&#39;, &#39;sat&#39;)</span>
<span class="sd">    &gt;&gt;&gt; student.add_cpds(intel_cpd, sat_cpd)</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.inference import GibbsSampling</span>
<span class="sd">    &gt;&gt;&gt; gibbs_chain = GibbsSampling(student)</span>
<span class="sd">    Sample from it:</span>
<span class="sd">    &gt;&gt;&gt; gibbs_chain.sample(size=3)</span>
<span class="sd">       intel  sat</span>
<span class="sd">    0      0    0</span>
<span class="sd">    1      0    0</span>
<span class="sd">    2      1    1</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">GibbsSampling</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">BayesianModel</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_get_kernel_from_bayesian_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">model</span><span class="p">,</span> <span class="n">MarkovModel</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_get_kernel_from_markov_model</span><span class="p">(</span><span class="n">model</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_get_kernel_from_bayesian_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the Gibbs transition models from a Bayesian Network.</span>
<span class="sd">        &#39;Probabilistic Graphical Model Principles and Techniques&#39;, Koller and</span>
<span class="sd">        Friedman, Section 12.3.3 pp 512-513.</span>

<span class="sd">        Parameters:</span>
<span class="sd">        -----------</span>
<span class="sd">        model: BayesianModel</span>
<span class="sd">            The model from which probabilities will be computed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">nodes</span><span class="p">())</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cardinalities</span> <span class="o">=</span> <span class="p">{</span><span class="n">var</span><span class="p">:</span> <span class="n">model</span><span class="o">.</span><span class="n">get_cpds</span><span class="p">(</span><span class="n">var</span><span class="p">)</span><span class="o">.</span><span class="n">variable_card</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">}</span>

        <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">:</span>
            <span class="n">other_vars</span> <span class="o">=</span> <span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="k">if</span> <span class="n">var</span> <span class="o">!=</span> <span class="n">v</span><span class="p">]</span>
            <span class="n">other_cards</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinalities</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">other_vars</span><span class="p">]</span>
            <span class="n">cpds</span> <span class="o">=</span> <span class="p">[</span><span class="n">cpd</span> <span class="k">for</span> <span class="n">cpd</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">cpds</span> <span class="k">if</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">cpd</span><span class="o">.</span><span class="n">scope</span><span class="p">()]</span>
            <span class="n">prod_cpd</span> <span class="o">=</span> <span class="n">factor_product</span><span class="p">(</span><span class="o">*</span><span class="n">cpds</span><span class="p">)</span>
            <span class="n">kernel</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">scope</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">prod_cpd</span><span class="o">.</span><span class="n">scope</span><span class="p">())</span>
            <span class="k">for</span> <span class="n">tup</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="nb">range</span><span class="p">(</span><span class="n">card</span><span class="p">)</span> <span class="k">for</span> <span class="n">card</span> <span class="ow">in</span> <span class="n">other_cards</span><span class="p">]):</span>
                <span class="n">states</span> <span class="o">=</span> <span class="p">[</span><span class="n">State</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">v</span><span class="p">,</span> <span class="n">s</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">other_vars</span><span class="p">,</span> <span class="n">tup</span><span class="p">)</span> <span class="k">if</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">scope</span><span class="p">]</span>
                <span class="n">prod_cpd_reduced</span> <span class="o">=</span> <span class="n">prod_cpd</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">states</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                <span class="n">kernel</span><span class="p">[</span><span class="n">tup</span><span class="p">]</span> <span class="o">=</span> <span class="n">prod_cpd_reduced</span><span class="o">.</span><span class="n">values</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">prod_cpd_reduced</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">transition_models</span><span class="p">[</span><span class="n">var</span><span class="p">]</span> <span class="o">=</span> <span class="n">kernel</span>

    <span class="k">def</span> <span class="nf">_get_kernel_from_markov_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Computes the Gibbs transition models from a Markov Network.</span>
<span class="sd">        &#39;Probabilistic Graphical Model Principles and Techniques&#39;, Koller and</span>
<span class="sd">        Friedman, Section 12.3.3 pp 512-513.</span>

<span class="sd">        Parameters:</span>
<span class="sd">        -----------</span>
<span class="sd">        model: MarkovModel</span>
<span class="sd">            The model from which probabilities will be computed.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">nodes</span><span class="p">())</span>
        <span class="n">factors_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">var</span><span class="p">:</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">}</span>
        <span class="k">for</span> <span class="n">factor</span> <span class="ow">in</span> <span class="n">model</span><span class="o">.</span><span class="n">get_factors</span><span class="p">():</span>
            <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">():</span>
                <span class="n">factors_dict</span><span class="p">[</span><span class="n">var</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">factor</span><span class="p">)</span>

        <span class="c1"># Take factor product</span>
        <span class="n">factors_dict</span> <span class="o">=</span> <span class="p">{</span><span class="n">var</span><span class="p">:</span> <span class="n">factor_product</span><span class="p">(</span><span class="o">*</span><span class="n">factors</span><span class="p">)</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">factors</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="k">else</span> <span class="n">factors</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                        <span class="k">for</span> <span class="n">var</span><span class="p">,</span> <span class="n">factors</span> <span class="ow">in</span> <span class="n">factors_dict</span><span class="o">.</span><span class="n">items</span><span class="p">()}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">cardinalities</span> <span class="o">=</span> <span class="p">{</span><span class="n">var</span><span class="p">:</span> <span class="n">factors_dict</span><span class="p">[</span><span class="n">var</span><span class="p">]</span><span class="o">.</span><span class="n">get_cardinality</span><span class="p">([</span><span class="n">var</span><span class="p">])[</span><span class="n">var</span><span class="p">]</span> <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">}</span>

        <span class="k">for</span> <span class="n">var</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">:</span>
            <span class="n">other_vars</span> <span class="o">=</span> <span class="p">[</span><span class="n">v</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span> <span class="k">if</span> <span class="n">var</span> <span class="o">!=</span> <span class="n">v</span><span class="p">]</span>
            <span class="n">other_cards</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinalities</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">other_vars</span><span class="p">]</span>
            <span class="n">kernel</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="n">factor</span> <span class="o">=</span> <span class="n">factors_dict</span><span class="p">[</span><span class="n">var</span><span class="p">]</span>
            <span class="n">scope</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="n">factor</span><span class="o">.</span><span class="n">scope</span><span class="p">())</span>
            <span class="k">for</span> <span class="n">tup</span> <span class="ow">in</span> <span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">(</span><span class="o">*</span><span class="p">[</span><span class="nb">range</span><span class="p">(</span><span class="n">card</span><span class="p">)</span> <span class="k">for</span> <span class="n">card</span> <span class="ow">in</span> <span class="n">other_cards</span><span class="p">]):</span>
                <span class="n">states</span> <span class="o">=</span> <span class="p">[</span><span class="n">State</span><span class="p">(</span><span class="n">var</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span> <span class="k">for</span> <span class="n">var</span><span class="p">,</span> <span class="n">s</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">other_vars</span><span class="p">,</span> <span class="n">tup</span><span class="p">)</span> <span class="k">if</span> <span class="n">var</span> <span class="ow">in</span> <span class="n">scope</span><span class="p">]</span>
                <span class="n">reduced_factor</span> <span class="o">=</span> <span class="n">factor</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="n">states</span><span class="p">,</span> <span class="n">inplace</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                <span class="n">kernel</span><span class="p">[</span><span class="n">tup</span><span class="p">]</span> <span class="o">=</span> <span class="n">reduced_factor</span><span class="o">.</span><span class="n">values</span> <span class="o">/</span> <span class="nb">sum</span><span class="p">(</span><span class="n">reduced_factor</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">transition_models</span><span class="p">[</span><span class="n">var</span><span class="p">]</span> <span class="o">=</span> <span class="n">kernel</span>

<div class="viewcode-block" id="GibbsSampling.sample"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.Sampling.GibbsSampling.sample">[docs]</a>    <span class="k">def</span> <span class="nf">sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">start_state</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">return_type</span><span class="o">=</span><span class="s2">&quot;dataframe&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Sample from the Markov Chain.</span>

<span class="sd">        Parameters:</span>
<span class="sd">        -----------</span>
<span class="sd">        start_state: dict or array-like iterable</span>
<span class="sd">            Representing the starting states of the variables. If None is passed, a random start_state is chosen.</span>
<span class="sd">        size: int</span>
<span class="sd">            Number of samples to be generated.</span>
<span class="sd">        return_type: string (dataframe | recarray)</span>
<span class="sd">            Return type for samples, either of &#39;dataframe&#39; or &#39;recarray&#39;.</span>
<span class="sd">            Defaults to &#39;dataframe&#39;</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sampled: A pandas.DataFrame or a numpy.recarray object depending upon return_type argument</span>
<span class="sd">            the generated samples</span>

<span class="sd">        Examples:</span>
<span class="sd">        ---------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.inference import GibbsSampling</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; model = MarkovModel([(&#39;A&#39;, &#39;B&#39;), (&#39;C&#39;, &#39;B&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; factor_ab = DiscreteFactor([&#39;A&#39;, &#39;B&#39;], [2, 2], [1, 2, 3, 4])</span>
<span class="sd">        &gt;&gt;&gt; factor_cb = DiscreteFactor([&#39;C&#39;, &#39;B&#39;], [2, 2], [5, 6, 7, 8])</span>
<span class="sd">        &gt;&gt;&gt; model.add_factors(factor_ab, factor_cb)</span>
<span class="sd">        &gt;&gt;&gt; gibbs = GibbsSampling(model)</span>
<span class="sd">        &gt;&gt;&gt; gibbs.sample(size=4, return_tupe=&#39;dataframe&#39;)</span>
<span class="sd">           A  B  C</span>
<span class="sd">        0  0  1  1</span>
<span class="sd">        1  1  0  0</span>
<span class="sd">        2  1  1  0</span>
<span class="sd">        3  1  1  1</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">start_state</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">random_state</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">start_state</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">set_start_state</span><span class="p">(</span><span class="n">start_state</span><span class="p">)</span>

        <span class="n">types</span> <span class="o">=</span> <span class="p">[(</span><span class="n">var_name</span><span class="p">,</span> <span class="s1">&#39;int&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">var_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">variables</span><span class="p">]</span>
        <span class="n">sampled</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">size</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">types</span><span class="p">)</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">recarray</span><span class="p">)</span>
        <span class="n">sampled</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">st</span> <span class="k">for</span> <span class="n">var</span><span class="p">,</span> <span class="n">st</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">])</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="p">(</span><span class="n">var</span><span class="p">,</span> <span class="n">st</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">):</span>
                <span class="n">other_st</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">st</span> <span class="k">for</span> <span class="n">v</span><span class="p">,</span> <span class="n">st</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="k">if</span> <span class="n">var</span> <span class="o">!=</span> <span class="n">v</span><span class="p">)</span>
                <span class="n">next_st</span> <span class="o">=</span> <span class="n">sample_discrete</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinalities</span><span class="p">[</span><span class="n">var</span><span class="p">])),</span>
                                          <span class="bp">self</span><span class="o">.</span><span class="n">transition_models</span><span class="p">[</span><span class="n">var</span><span class="p">][</span><span class="n">other_st</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">State</span><span class="p">(</span><span class="n">var</span><span class="p">,</span> <span class="n">next_st</span><span class="p">)</span>
            <span class="n">sampled</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">st</span> <span class="k">for</span> <span class="n">var</span><span class="p">,</span> <span class="n">st</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">])</span>

        <span class="k">return</span> <span class="n">_return_samples</span><span class="p">(</span><span class="n">return_type</span><span class="p">,</span> <span class="n">sampled</span><span class="p">)</span></div>

<div class="viewcode-block" id="GibbsSampling.generate_sample"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.Sampling.GibbsSampling.generate_sample">[docs]</a>    <span class="k">def</span> <span class="nf">generate_sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">start_state</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Generator version of self.sample</span>

<span class="sd">        Return Type:</span>
<span class="sd">        ------------</span>
<span class="sd">        List of State namedtuples, representing the assignment to all variables of the model.</span>

<span class="sd">        Examples:</span>
<span class="sd">        ---------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.discrete import DiscreteFactor</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.sampling import GibbsSampling</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.models import MarkovModel</span>
<span class="sd">        &gt;&gt;&gt; model = MarkovModel([(&#39;A&#39;, &#39;B&#39;), (&#39;C&#39;, &#39;B&#39;)])</span>
<span class="sd">        &gt;&gt;&gt; factor_ab = DiscreteFactor([&#39;A&#39;, &#39;B&#39;], [2, 2], [1, 2, 3, 4])</span>
<span class="sd">        &gt;&gt;&gt; factor_cb = DiscreteFactor([&#39;C&#39;, &#39;B&#39;], [2, 2], [5, 6, 7, 8])</span>
<span class="sd">        &gt;&gt;&gt; model.add_factors(factor_ab, factor_cb)</span>
<span class="sd">        &gt;&gt;&gt; gibbs = GibbsSampling(model)</span>
<span class="sd">        &gt;&gt;&gt; gen = gibbs.generate_sample(size=2)</span>
<span class="sd">        &gt;&gt;&gt; [sample for sample in gen]</span>
<span class="sd">        [[State(var=&#39;C&#39;, state=1), State(var=&#39;B&#39;, state=1), State(var=&#39;A&#39;, state=0)],</span>
<span class="sd">         [State(var=&#39;C&#39;, state=0), State(var=&#39;B&#39;, state=1), State(var=&#39;A&#39;, state=1)]]</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">start_state</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">random_state</span><span class="p">()</span>
        <span class="k">elif</span> <span class="n">start_state</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">set_start_state</span><span class="p">(</span><span class="n">start_state</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">size</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">j</span><span class="p">,</span> <span class="p">(</span><span class="n">var</span><span class="p">,</span> <span class="n">st</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">):</span>
                <span class="n">other_st</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">st</span> <span class="k">for</span> <span class="n">v</span><span class="p">,</span> <span class="n">st</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="k">if</span> <span class="n">var</span> <span class="o">!=</span> <span class="n">v</span><span class="p">)</span>
                <span class="n">next_st</span> <span class="o">=</span> <span class="n">sample_discrete</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">cardinalities</span><span class="p">[</span><span class="n">var</span><span class="p">])),</span>
                                          <span class="bp">self</span><span class="o">.</span><span class="n">transition_models</span><span class="p">[</span><span class="n">var</span><span class="p">][</span><span class="n">other_st</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">State</span><span class="p">(</span><span class="n">var</span><span class="p">,</span> <span class="n">next_st</span><span class="p">)</span>
            <span class="k">yield</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span><span class="p">[:]</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>